package vn.com.pavibook.admin.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import vn.com.pavibook.admin.service.ManageBannerService;
import vn.com.pavibook.domain.model.Banner;
import vn.com.pavibook.domain.model.PagingInfo;
import vn.com.pavibook.domain.model.SearchResult;
import vn.com.pavibook.model.vo.BannerVO;

@Controller
public class ManageBannerController {

	private static final int offset = 10;
	
	private static final Logger logger = LoggerFactory.getLogger(ManageBookController.class);
	
	@Autowired
	private ManageBannerService manageBannerService;
	
	//info
	@RequestMapping(value = "/admin/banner-info", method=RequestMethod.GET)
	public String openManageBanner(Model model){
		logger.debug("manage banner");
		
		PagingInfo pagingInfo = new PagingInfo(offset, 1);
		SearchResult<Banner> result = manageBannerService.getBanner(pagingInfo, null);
		List<Banner> listBanner = result.getSearchResult();
		PagingInfo pagingInfoNew = result.getPagingInfo();
		model.addAttribute("listBanner", listBanner);
		model.addAttribute("pagingInfo", pagingInfoNew);
		
		return "admin/banner-info";
	}
	
	//search
	@RequestMapping(value = "/admin/searchBanner", method = RequestMethod.POST)
	public String searchBanner(Model model, 
			@RequestParam("pageIndex") int pageIndex, @RequestParam("keyword") String keyword) {
		
		PagingInfo pagingInfo = new PagingInfo(offset, pageIndex);
		SearchResult<Banner> result = new SearchResult<Banner>();
		if(StringUtils.isEmpty(keyword)){
			result = manageBannerService.getBanner(pagingInfo, null);
		} else {
			result = manageBannerService.getBanner(pagingInfo, keyword);
		}
		List<Banner> listBanner = result.getSearchResult();
		
		model.addAttribute("listBanner", listBanner);
		model.addAttribute("pagingInfo", pagingInfo);
		model.addAttribute("keyword", keyword);
		return "admin/banner-info";
	}
	
	//create add new
	@RequestMapping(value = "/admin/add-banner", method=RequestMethod.GET)
	public String createAddNewBanner(Model model){
		logger.debug("controller create banner");
		BannerVO bannerVO = new BannerVO();
		model.addAttribute("bannerVO", bannerVO);
		
		return "admin/add-banner";
	}
	
	//save add new
	@RequestMapping(value = "/admin/save-banner")
	public String saveBook(Model model,@Valid @ModelAttribute("bannerVO") BannerVO bannerVO,BindingResult result, HttpServletRequest request){
		logger.debug("controller saveBooktype");
		if(result.hasErrors()){
			return "admin/add-banner";
		}
		boolean resultUpload = manageBannerService.uploadBanner(model, bannerVO, request);
		if(!resultUpload){
			return "admin/add-banner";
		}else{
			manageBannerService.saveBanner(bannerVO);
		}
		model.addAttribute("addStatus", true);
		return "admin/add-banner";
	}
	
	//delete
	@RequestMapping(value = "/admin/delete-banner", method=RequestMethod.GET)
	public String deleteBanner(Model model, @RequestParam("id") int id){
		logger.debug("controller delete banner");
		manageBannerService.deleteBanner(id);
		return "redirect:banner-info";
	}
	
	//view
	@RequestMapping(value="admin/ajax/viewDetailBanner", method=RequestMethod.GET)
	public ModelAndView viewDetailBanner(@RequestParam("id") int id){
		ModelAndView model = new ModelAndView("admin/view-detail-banner");
		Banner banner = manageBannerService.getBannerDetail(id);
		String createDate = "";
		if(banner != null){
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
			Date date = banner.getCreateDate();
			createDate = simpleDateFormat.format(date);
		}
		model.addObject("createDate", createDate);
		model.addObject("banner", banner);
		return model;
	}
}
